import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { ServiceProvider } from '../../providers/service/service'
import { ApiProvider } from '../../providers/api/api'
import { Md5 } from 'ts-md5/dist/md5';




@Component({
    selector: 'page-TabRank',
    templateUrl: 'tab-rank.html'
})
export class TabRankPage {
    public index: Number;
    public chooseArray: Array<string> = ['day', 'week', 'month'];
    public choose: String;
    // public time: String;
    public resData: any;
    public Arr: Array<any> = [];
    public one: any = {};
    public two: any = {};
    public three: any = {};

    constructor(
        public navCtrl: NavController, 
        public service: ServiceProvider, 
        public Api: ApiProvider ) {
        this.choose = this.chooseArray[0];
        // this.time = new Date().toLocaleTimeString();

    };

    ionViewDidEnter(){
        this.PostRankD();
    }

    PostRankD(){
        let timeStamp = (new Date()).getTime();
        this.Api.rankD({
            times: timeStamp,
            token: Md5.hashStr('HalfPriceMall' + timeStamp),
            rank: 'rank',
            page: 1,
            count: 20
        }).then(res=>{
            this.resData = res;
            if( this.resData.code == 1 ) {
                this.one = this.resData.data[0];
                this.two = this.resData.data[1];
                this.three = this.resData.data[2];
                this.Arr = [];
                for( let k=3; k < this.resData.data.length; k++ ) {
                    this.Arr.push(this.resData.data[k])
                };
            }
        })
    };

    PostRankW(){
        let timeStamp = (new Date()).getTime();
        this.Api.rankW({
            times: timeStamp,
            token: Md5.hashStr('HalfPriceMall' + timeStamp),
            rank: 'rank',
            page: 1,
            count: 20
        }).then(res=>{
            this.resData = res;
            if( this.resData.code == 1 ) {
                this.one = this.resData.data[0];
                this.two = this.resData.data[1];
                this.three = this.resData.data[2];
                this.Arr = [];
                for( let k=3; k < this.resData.data.length; k++ ) {
                    this.Arr.push(this.resData.data[k])
                };
            }
        })
    };

    PostRankM(){
        let timeStamp = (new Date()).getTime();
        this.Api.rankM({
            times: timeStamp,
            token: Md5.hashStr('HalfPriceMall' + timeStamp),
            rank: 'rank',
            page: 1,
            count: 20
        }).then(res=>{
            this.resData = res;
            if( this.resData.code == 1 ) {
                this.one = this.resData.data[0];
                this.two = this.resData.data[1];
                this.three = this.resData.data[2];
                this.Arr = [];
                for( let k=3; k < this.resData.data.length; k++ ) {
                    this.Arr.push(this.resData.data[k])
                };
            }
        })
    };

    segmentChanged(e) {
        console.log( e.value )
        switch (e.value) {
            case 'day':
                this.PostRankD();
                break;

            case 'week':
                this.PostRankW();
                break;

            case 'month':
                this.PostRankM();
                break;
        }

    };
    // doRefresh(e) {
    //     console.log( e );
    //     this.time = this.service.stotime(e._lastCheck);
    //     setTimeout(() => {
    //         console.log('Async operation has ended');
    //         e.complete();
    //     }, 1000);
    // }

}
